﻿using System;
using System.Collections.Concurrent;

namespace RabbitMQExtension
{
    public sealed class LogManager
    {
        private static readonly ConcurrentDictionary<string, ILog> _logDic = new ConcurrentDictionary<string, ILog>();

        private LogManager()
        {
        }

        public static ILog GetLogger(Type type)
        {
            if (type == null)
                return GetLogger(string.Empty);

            return GetLogger(type.FullName);
        }

        public static ILog GetLogger(string typeName)
        {
            if (string.IsNullOrEmpty(typeName) || string.IsNullOrWhiteSpace(typeName))
                typeName = "NoName";

            return _logDic.GetOrAdd(typeName, _ =>
            {
                var log = new DefaultLogger(typeName);
                return log;
            });
        }

        public static ILog GetLogger(string typeName, ILog log)
        {
            if (string.IsNullOrEmpty(typeName) || string.IsNullOrWhiteSpace(typeName))
                typeName = "NoName";

            if (log != null)
            {
                _logDic[typeName] = log;
                return log;
            }
            else
                return GetLogger(typeName);
        }
    }
}